#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>

using namespace std;

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};

int getMinimumDifference(TreeNode* root) {
    int res = INT_MAX, pre = -1;
    dfs(root, pre, res);
    return res;
}

void dfs(TreeNode* root, int& pre, int& res)
{
    if (root == nullptr) return;

    dfs(root->left, pre, res);
    if (pre == -1)   pre = root->val;
    else
    {
        res = min(res, root->val - pre);
        pre = root->val;
    }
    dfs(root->right, pre, res);
}